﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Collections.ObjectModel;

namespace SilverlightPlayer
{
    public partial class Playlist : UserControl
    {
        DispatcherTimer animationTimer = new DispatcherTimer();
        double desiredScrollPosition;
        public Playlist()
        {
            InitializeComponent();
            TrackList = new ObservableCollection<Track>();
            animationTimer.Interval = TimeSpan.FromMilliseconds(50);
            animationTimer.Tick += DoAnimation;
            TrackList.CollectionChanged += TrackList_CollectionChanged;
            
        }

        void TrackList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            stackContents.Children.Clear();
            foreach (Track t in TrackList)
            {
                PlaylistItem item = new PlaylistItem();
                item.Track = t;
                item.Margin = new Thickness(10, 0, 10, 0);
                item.IsSelected += (s, ev) => { CurrentPlayingTrackIndex = TrackList.IndexOf(ev.TrackSelected); };
                item.IsHovered += item_IsHovered;
                stackContents.Children.Add(item);

            }
        }

        private Track _currentPlayingTrack;
        public Track CurrentPlayingTrack
        {
            get
            {
                return _currentPlayingTrack;
            }
            private set
            {
                 _currentPlayingTrack = value;
                  if (TrackIsSelected != null) TrackIsSelected(this, new TrackIsSelectedEventArgs(_currentPlayingTrack));
            }

        }


        int _currentPlayingTrackIndex;
        public int CurrentPlayingTrackIndex
        {
            get
            {
                return _currentPlayingTrackIndex;
            }
            set
            {
                if (value >= 0 && value < TrackList.Count)
                {
                    _currentPlayingTrackIndex = value;
                  
                    CurrentPlayingTrack = TrackList[_currentPlayingTrackIndex];
                    foreach (UIElement el in stackContents.Children)
                    {
                        if (el is PlaylistItem)
                        {
                            PlaylistItem pi = (el as PlaylistItem);
                            if (pi.Track != CurrentPlayingTrack) pi.IsPlaying = false; else pi.IsPlaying = true;
                            
                        }
                    }
                }
            }
        }

        void item_IsHovered(object sender, TrackIsSelectedEventArgs e)
        {
            txtTrackName.Text = e.TrackSelected.Info;
        }

        void DoAnimation(object sender, EventArgs e)
        {
            double currentScrollPosition = scrollView.HorizontalOffset;
            double speed = (desiredScrollPosition - currentScrollPosition) / 2;
            scrollView.ScrollToHorizontalOffset(currentScrollPosition + speed);
            if (Math.Abs(desiredScrollPosition - currentScrollPosition) < 1) animationTimer.Stop();
        }

        private void LayoutRoot_MouseMove(object sender, MouseEventArgs e)
        {
            if(!animationTimer.IsEnabled) animationTimer.Start();
            desiredScrollPosition = (e.GetPosition(this).X / this.ActualWidth) * scrollView.ScrollableWidth;
        }


        public ObservableCollection<Track> TrackList { get; set; }
        public event EventHandler<TrackIsSelectedEventArgs> TrackIsSelected;


        internal void play()
        {
            CurrentPlayingTrackIndex = CurrentPlayingTrackIndex;
        }
    }
}
